﻿using CYZH.CommandAssembly.JsonObject;
using CYZH.Common;
using CYZH.Service.Drought;
using SuperWebSocket;
using SuperWebSocket.SubProtocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CYZH.CommandAssembly.Command
{
    public class TVDI : JsonSubCommand<MODISParam>
    {
        protected override void ExecuteJsonCommand(WebSocketSession session, MODISParam commandInfo)
        {

            double[] ext = { 0, 0, 0, 0 };
            OutRasterConfigElement rce = Common.ConfigHelper.GetConfigParamByXzqhCode(commandInfo.XzqhCode);

            string strminx = rce.Extents;
            string[] strext = strminx.Split(',');

            double MinX = Convert.ToDouble(strext[0]);
            double MinY = Convert.ToDouble(strext[1]);
            double MaxX = Convert.ToDouble(strext[2]);
            double MaxY = Convert.ToDouble(strext[3]);
            ext[0] = MinX;// { MinX, MinY, MaxX, MaxY };
            ext[1] = MinY;
            ext[2] = MaxX;
            ext[3] = MaxY;

            string aoifile = rce.ShpFile;
            string cldaoiFile = rce.ChildShpFile;
            string layername = commandInfo.XzqhCode + "_" + commandInfo.FileTime + "_tvdi";
            string strextents = ext[0] + ", " + ext[1] + ", " + ext[2] + ", " + ext[3];
            ResultState resultstate2 = new ResultState();
            resultstate2.process = "30";
            resultstate2.value = "正在计算...";
            SendJsonMessage(session, resultstate2);
            //查询数据是否已经做过
            List<ModisResultBean> res = CommonService.queryRasterResult(commandInfo.XzqhCode, commandInfo.FileTime, "TVDI", "xun",null);
            if (res.Count != 0)
            {
                //string filename1 = res[0].FileName;
                //string filepath = FileOper.GetExitsModisFilePath("MOD11A1", filename1, "TVDI", "map");
                //string unixfile1 = filepath.Replace('\\', '/');
                MapFileResult resultstate9 = new MapFileResult();
                resultstate9.process = "100";
                resultstate9.state = "0";
                resultstate9.value = "success";
                resultstate9.LayerName = res[0].LayerName;
                resultstate9.Extents = strextents;
                //resultstate9.MapFile = unixfile1;
                SendJsonMessage(session, resultstate9);
                Logger.Singleton.Debug(String.Format("TDVI结果已经存在."));
                return;
            }
           
            string ndvifile = "", lstfile="";
            foreach (string f in commandInfo.MODISFileList)
            {
                if (f.Contains("ndvi"))
                {
                    //ndvifile = FileOper.GenSHINDVI(f);
                    ndvifile = FileOper.GetExitsModisFilePath(f, "NDVI");
                }
                if (f.Contains("lst"))
                {
                    lstfile = FileOper.GetExitsModisFilePath(f, "LST");
                }

            }
            ResultState resultstate3 = new ResultState();
            resultstate3.process = "60";
            resultstate3.value = "正在计算...";
            SendJsonMessage(session, resultstate3);
            if (!File.Exists(ndvifile) || !File.Exists(lstfile))
            {
                Logger.Singleton.Debug("文件不存在...");
                ResultState errstate = new ResultState();
                errstate.process = "100";
                errstate.state = "-1";
                errstate.value = ndvifile + "或" + lstfile + "文件不存在...";
                SendJsonMessage(session, errstate);
                return;
            }
            string aoiShpFile = FileOper.GenAOIFilePath(aoifile);
            string cldaoiShpFile = FileOper.GenAOIFilePath(cldaoiFile); 
            string sfilename = FileOper.GenModisFilePath( layername, "TVDI");

            TVDIServide tvdi = new TVDIServide();
            int rst = tvdi.CaculateTVDI(ndvifile, lstfile, aoiShpFile, sfilename);
            if (rst != 0)
            {
                ResultState errstate = new ResultState();
                errstate.process = "100";
                errstate.state = "-1";
                errstate.value = "计算TVDI出错!";
                SendJsonMessage(session, errstate);
                return;
            }
            //生成map文件
            string mapfile = MapFileUtils.GenMapFile("TVDI", strextents.Replace(',', ' '), layername);
            //生成统计信息，按当前行政区划的下一级统计
            string str_static = CommonService.RegionaStatistics(sfilename, cldaoiShpFile);
            //保存到数据库
            CommonService.saveRasterResult(mapfile, commandInfo.FileTime, commandInfo.XzqhCode, layername, "TVDI", commandInfo.FileTime.Substring(4, 2), "", commandInfo.FileTime.Substring(6), "xun", str_static,null);

            //string unixfile = mapfile.Replace('\\', '/');
            MapFileResult resultstate10 = new MapFileResult();
            resultstate10.process = "100";
            resultstate10.state = "0";
            resultstate10.value = "success";
            resultstate10.LayerName = layername;
            resultstate10.Extents = strextents;
            //resultstate10.MapFile = unixfile;
            SendJsonMessage(session, resultstate10);
            Logger.Singleton.Debug(String.Format("计算TVDI结束，生成MAP文件路径：{0}.", mapfile));
            

        }
    }
}
